package com.ztjava.ask.controller;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.plugin.activerecord.Page;
import com.ztjava.ask.model.AskCatalog;
import com.ztjava.ask.model.AskQuestion;
import com.ztjava.ask.service.AskService;
import com.ztjava.ask.validator.AddAnswerValidator;
import com.ztjava.ask.validator.AddQuestionValidator;
import com.ztjava.ask.validator.AddReplyValidator;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.client.member.ApiClient;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.ext.auth.bean.AuthSession;
import com.ztjava.ext.auth.tool.AuthTool;

@ControllerClass(client=ApiClient.class,path="/ask")
@UserController(portal=Constant.P_MEMBER,token=AuthTool.para)
public class AskApiController extends BaseController{
	private static AskService askService = Duang.duang(AskService.class);
	/***
	 * 问题列表
	 */
	@UserMethod(option="ASK_AUTH")
	public void questionList(){
		Map<String, String[]> params = new HashMap<>(getParaMap());
		Integer catalog_id = getParaToInt("catalog_id",0);
		if(catalog_id>0){
			AskCatalog askCatalog = AskCatalog.dao.findById(catalog_id);
			if(askCatalog==null||askCatalog.getStr("enable_flag").equals("F")){
				renderError("目录不存在");
				return;
			}
			params.put("num", new String[]{askCatalog.getStr("num")});
		}
		
		String keyword = getPara("keyword","");
		if(!keyword.equals("")){
			params.put("title", new String[]{keyword});
		}
		setAttr(Constant.RESULT,Constant.SUCCESS);
		setAttr(Constant.MESSAGE, "操作成功");
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		if(authSession!=null){
			setAttr("pd",askService.getQuestionPage(client, params, authSession.getId(), false));
		}else {
			setAttr("pd",askService.getQuestionPage(client, params));
		}
		renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"pd"});
	}
	
	@UserMethod(option="ASK_AUTH")
	public void questionAllList(){
		Map<String, String[]> params = new HashMap<>(getParaMap()) ;
		Integer catalog_id = getParaToInt("catalog_id",0);
		if(catalog_id>0){
			AskCatalog askCatalog = AskCatalog.dao.findById(catalog_id);
			if(askCatalog==null||askCatalog.getStr("enable_flag").equals("F")){
				renderError("目录不存在");
				return;
			}
			params.put("num", new String[]{askCatalog.getStr("num")});
		}
		Page<AskQuestion> pd = askService.getQuestionPage(client, params);
		
		setAttr(Constant.RESULT,Constant.SUCCESS);
		setAttr(Constant.MESSAGE, "操作成功");
		setAttr("list",pd.getList());
		renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"list"});
	}
	/***
	 * 我的问题列表
	 */
	@UserMethod
	public void myQuestionList(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		Map<String, String[]> params = new HashMap<>(getParaMap());
		Integer catalog_id = getParaToInt("catalog_id",0);
		if(catalog_id>0){
			AskCatalog askCatalog = AskCatalog.dao.findById(catalog_id);
			if(askCatalog==null||askCatalog.getStr("enable_flag").equals("F")){
				renderError("目录不存在");
				return;
			}
			params.put("num", new String[]{askCatalog.getStr("num")});
		}
		String keyword = getPara("keyword","");
		if(!keyword.equals("")){
			params.put("title", new String[]{keyword});
		}
		params.put("create_user",new String[]{authSession.getId().toString()});
		setAttr(Constant.RESULT,Constant.SUCCESS);
		setAttr(Constant.MESSAGE,"操作成功");
		setAttr("pd",askService.getQuestionPage(client, params));
		renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"pd"});
	}
	/***
	 * 收藏列表
	 */
	@UserMethod
	public void myFavList(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		Map<String, String[]> params = new HashMap<>(getParaMap());
		Integer catalog_id = getParaToInt("catalog_id",0);
		if(catalog_id>0){
			AskCatalog askCatalog = AskCatalog.dao.findById(catalog_id);
			if(askCatalog==null||askCatalog.getStr("enable_flag").equals("F")){
				renderError("目录不存在");
				return;
			}
			params.put("num", new String[]{askCatalog.getStr("num")});
		}
		String keyword = getPara("keyword","");
		if(!keyword.equals("")){
			params.put("title", new String[]{keyword});
		}
		setAttr(Constant.RESULT,Constant.SUCCESS);
		setAttr(Constant.MESSAGE,"操作成功");
		setAttr("pd",askService.getQuestionPage(client, params,authSession.getId(),true));
		renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"pd"});
	}
	/**
	 * 问题信息
	 */
	@UserMethod(option="ASK_AUTH")
	public void question(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		askService.showQuestion(this, getParaToInt("id"), authSession);
		setAttr("answerList", askService.getAnswerList(getParaToInt("id"), authSession));
		setAttr(Constant.RESULT,Constant.SUCCESS);
		setAttr(Constant.MESSAGE,"操作成功");
		renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,
				"askQuestion","answerList","askFav","catalogPath"});
	}
	/***
	 * 提问
	 */
	@UserMethod
	@Before({POST.class,AddQuestionValidator.class})
	public void addQuestion(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		renderRet(askService.insertQuestion(getParaToInt("catalog_id"),  
				getPara("title"), 
				getPara("content"), 
				getPara("images"), 
				getParaToInt("score",0), 
				authSession.getId()));
	}
	/***
	 * 回答
	 */
	@UserMethod
	@Before({POST.class,AddAnswerValidator.class})
	public void addAnswer(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		renderRet(askService.insertAnswer(getParaToInt("question_id"), 
				getPara("content"), 
				getPara("image"),
				authSession.getId()));
	}
	/***
	 * 回复
	 */
	@UserMethod
	@Before({POST.class,AddReplyValidator.class})
	public void addReply(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		renderRet(askService.insertReply(getParaToInt("answer_id"), getPara("content"), getPara("image"), authSession.getId()));
	}
	/***
	 * 分类列表
	 */
	@UserMethod(option="ASK_AUTH")
	public void treeCatalogList(){
		Map<String, String[]> params = new HashMap<>(getParaMap());
		setAttr("catalogList", askService.treeCatalog(params));
		setAttr(Constant.RESULT,Constant.SUCCESS);
		setAttr(Constant.MESSAGE,"操作成功");
		renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"catalogList"});
	}
	
	@UserMethod(option="ASK_AUTH")
	public void catalogList(){
		Map<String, String[]> params = new HashMap<>(getParaMap());
		setAttr("catalogList", askService.catalogList(params));
		setAttr(Constant.RESULT,Constant.SUCCESS);
		setAttr(Constant.MESSAGE,"操作成功");
		renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"catalogList"});
	}
	
	/***
	 * 采纳答案
	 */
	@UserMethod
	@Before(POST.class)
	public void good(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		renderRet(askService.goodAnswer(getParaToInt("answer_id"),authSession.getId()));
	}
	/***
	 * 收藏
	 */
	@UserMethod
	@Before(POST.class)
	public void fav(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		renderRet(askService.updateFav(getParaToInt("question_id"), authSession.getId()));
	}
	/***
	 * 赞
	 */
	@UserMethod
	@Before(POST.class)
	public void ding(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		renderRet(askService.updateDing(getParaToInt("answer_id",0), authSession.getId()));
	}

	@UserMethod
	public void myAnswerList(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		Map<String,String[]> params = new HashMap<String, String[]>(getParaMap());
		Integer catalog_id = getParaToInt("catalog_id",0);
		if(catalog_id>0){
			AskCatalog askCatalog = AskCatalog.dao.findById(catalog_id);
			if(askCatalog==null||askCatalog.getStr("enable_flag").equals("F")){
				renderError("目录不存在");
				return;
			}
			params.put("num", new String[]{askCatalog.getStr("num")});
		}
		String keyword = getPara("keyword","");
		if(!keyword.equals("")){
			params.put("title", new String[]{keyword});
		}
		params.put("create_user", new String[]{authSession.getId()+""});
		renderPageJson(askService.answerPage(client, params));
	}
	
}
